<template>
  <eip-modal :visible="visible" :title="title" @close="cancel" :width="600">
    <template slot="body">
      <a-input
        v-model="resetPassword.value"
        placeholder="请输入重置密码值"
      ></a-input>
      <a-alert type="warning" style="margin-top: 18px">
        <template slot="description">
          密码规则:<br />
          <div v-for="(item, index) in resetPassword.rule" :key="index">
            {{ index + 1 }}, {{ item }}
          </div>
        </template>
      </a-alert>
    </template>
    <template slot="footer">
      <a-space>
        <a-button @click="cancel"><a-icon type="close" />取消</a-button>
        <a-button
          key="submit"
          type="primary"
          :loading="resetPassword.loading"
          @click="save"
          ><a-icon type="save" />提交</a-button
        >
      </a-space>
    </template>
  </eip-modal>
</template>

<script>
import { resetPassword } from "@/services/system/user/list";
import { header } from "@/services/system/config/index";
export default {
  name: "resetPassword",
  data() {
    return {
      resetPassword: {
        loading: false,
        title: null,
        value: "",
        id: this.userId,
        rule: [],
      },

      loading: false,
    };
  },

  created() {
    this.initSystemConfig();
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
    },
    userId: {
      type: String,
    },
  },

  methods: {
    /**
     * 获取系统配置
     */
    initSystemConfig() {
      let that = this;
      header().then((result) => {
        if (result.code === this.eipResultCode.success) {
          let data = result.data;
          //是否有时间
          let rule = [];
          if (data.systemPasswordLength) {
            rule.push(
              "密码长度需大于等于" + data.systemPasswordLength + "个字符串"
            );
          }

          if (
            data.systemPasswordHaveNumber &&
            data.systemPasswordHaveNumber == "true"
          ) {
            rule.push(
              "必须包含至少" + data.systemPasswordHaveNumberLength + "个数字"
            );
          }

          if (
            data.systemPasswordHaveLetters &&
            data.systemPasswordHaveLetters == "true"
          ) {
            rule.push(
              "必须包含至少" + data.systemPasswordHaveLettersLength + "个字母"
            );
          }

          if (
            data.systemPasswordHaveSpecialCharacters &&
            data.systemPasswordHaveSpecialCharacters == "true"
          ) {
            rule.push(
              "必须包含至少" +
                data.systemPasswordHaveSpecialCharactersLength +
                "个特殊字符"
            );
          }
          that.resetPassword.rule = rule;
        }
      });
    },

    /**
     * 重置密码保存
     */
    save() {
      let that = this;
      if (that.resetPassword.value == "") {
        that.$message.error("请输入密码值");
        return false;
      }
      that.$message.loading("正在重置密码", 0);
      let param = {
        id: that.resetPassword.id,
        encryptPassword: that.resetPassword.value,
      };
      resetPassword(param).then((result) => {
        that.$message.destroy();
        if (result.code == that.eipResultCode.success) {
          that.resetPassword.visible = false;
          that.$message.success(result.msg);
          that.$emit("ok");
          that.cancel();
        } else {
          that.$message.error(result.msg);
        }
      });
    },

    /**
     * 取消
     */
    cancel() {
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="less"></style>
